package com.hellohoper.script.config.engine.container;

import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;

import javax.script.ScriptEngine;
import java.util.Map;

/**
 * 脚本容器
 * Created by hk on 17/8/15.
 */
public class GroovyEngineContainer implements EngineContainer {

    // 脚本容器
    private Map<String, ScriptEngine> localGroovyEngineContainer = Maps.newConcurrentMap();

    @Override
    public void addEngine(String appName, String scriptName, ScriptEngine scriptEngine) {
        if (StringUtils.isEmpty(appName) || StringUtils.isEmpty(scriptName) || scriptEngine == null) {
            return;
        }
        String key = appName + scriptName;
        localGroovyEngineContainer.put(key, scriptEngine);
    }

    @Override
    public void removeEngine(String appName, String scriptName) {
        if (StringUtils.isEmpty(appName) || StringUtils.isEmpty(scriptName)) {
            return;
        }
        String key = appName + scriptName;
        if (localGroovyEngineContainer.containsKey(key)) {
            localGroovyEngineContainer.remove(key);
        }
    }

    @Override
    public ScriptEngine getEngine(String appName, String scriptName) {
        if (StringUtils.isEmpty(appName) || StringUtils.isEmpty(scriptName)) {
            return null;
        }
        String key = appName + scriptName;
        if (localGroovyEngineContainer.containsKey(key)) {
            return localGroovyEngineContainer.get(key);
        }
        return null;
    }
}


